export default class Helper {

    private static timerSet = {};
    /** 防抖函数 */
    public static debounce(method: Function, scope: any, delay: number, uuid: string) {
        if (this.timerSet[uuid]) clearTimeout(this.timerSet[uuid]);
        this.timerSet[uuid] = setTimeout(() => {
            method.apply(scope);
        }, delay);
    }

    /** 进阶防抖函数 */
    public static debounce2(func, wait, immediate?) {
        let timeout, args, context, timestamp, result, later = function () {
            let last = new Date().getTime() - timestamp;
            if (last < wait && last >= 0) timeout = setTimeout(later, wait - last); else {
                timeout = null;
                if (!immediate) {
                    result = func.apply(context, args);
                    timeout || (context = args = null);
                }
            }
        };
        return function () {
            context = this;
            args = arguments;
            timestamp = new Date().getTime();
            let callNow = immediate && !timeout;
            timeout || (timeout = setTimeout(later, wait));
            if (callNow) {
                result = func.apply(context, args);
                context = args = null;
            }
            return result;
        };
    }

}